package me.phelps.library;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import java.util.ArrayList;

import uk.co.senab.photoview.PhotoView;
import uk.co.senab.photoview.PhotoViewAttacher;


public class ZoomGalleryAdapter extends PagerAdapter {

	private ArrayList<ZoomImageModel> mZoomImageList;

	private ZoomGalleryTapListener mListener;
	private ZoomGalleryInstantiateItem mItemListener;
	int photoViewPadding[] = new int[4];


	public void setPhotoViewPadding(int[] photoViewPadding) {
		if (photoViewPadding.length >= 4) {
			this.photoViewPadding = photoViewPadding;
		}
	}

	public ZoomGalleryAdapter(ZoomGalleryTapListener listener, ZoomGalleryInstantiateItem itemListener) {
		mListener = listener;
		mItemListener = itemListener;
	}

	public void update(ArrayList<ZoomImageModel> zoomImageList) {
		mZoomImageList = zoomImageList;
		notifyDataSetChanged( );
	}

	@Override
	public int getCount() {
		return mZoomImageList == null ? 0 : mZoomImageList.size( );
	}

	@Override
	public View instantiateItem(ViewGroup container, final int position) {

		PhotoView imageView = new PhotoView(container.getContext( ));
		imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		imageView.setPadding(photoViewPadding[0], photoViewPadding[1], photoViewPadding[2], photoViewPadding[3]);
		imageView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener( ) {
			@Override
			public void onPhotoTap(View view, float v, float v2) {
				if (mListener != null) {
					mListener.tap(position);
				}
			}
		});
		imageView.setOnViewTapListener(new PhotoViewAttacher.OnViewTapListener( ) {
			@Override
			public void onViewTap(View view, float v, float v2) {
				if (mListener != null) {
					mListener.tap(position);
				}
			}
		});

		RelativeLayout rll = new RelativeLayout(container.getContext( ));
		rll.addView(imageView, RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
		RelativeLayout.LayoutParams loadingRllp = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
		loadingRllp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
		ImageView img_loading = new ImageView(container.getContext( ));
		img_loading.setScaleType(ImageView.ScaleType.CENTER);
		img_loading.setImageResource(R.drawable.rotate_pro);
		img_loading.setVisibility(View.GONE);
		rll.addView(img_loading, loadingRllp);
		container.addView(rll, ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.MATCH_PARENT);
		if (mItemListener != null) {
			mItemListener.onItemInstantiate(container, position, imageView, img_loading, mZoomImageList.get(position));
		}
		return rll;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public int getItemPosition(Object object) {
		return POSITION_NONE;
	}

	public interface ZoomGalleryTapListener {
		public void tap(int position);
	}

	public interface ZoomGalleryInstantiateItem {
		void onItemInstantiate(ViewGroup container, final int position, PhotoView view, ImageView img_loading,
							   ZoomImageModel model);
	}

}
